﻿define(['jquery', 'swfupload', 'standardArtDialog', 'WdatePicker', 'category'], function ($, SWFupload, standardArtDialog, datePicker, category) {
    // 汉字全角算两个字符计算字符长度
    var getLength = function (value) {
        var replaceVal = value.replace(/[^\u0000-\u00ff]/g, "11");
        return replaceVal.length;
    };
    var normalDialog = function (msg) {
        standardArtDialog.dialog({
            drag: true,
            lock: true,
            width: 310,
            height: 50,
            title: '提示',
            content: msg,
            ok: function () {
                this.close();
            }
        });
    };
    // 文件选择框关闭时触发
    var fileDialogComplete = function (selected, queued) {
        if (selected > 1) {
            normalDialog('最多只能上传1张图片');
            return;
        }
        this.startUpload();
    };
    // 文件上传成功后触发
    var uploadSuccess = function (file, serverData) {
        var result = jQuery.parseJSON(serverData);
        if (!result)
            normalDialog("未能上传成功，请刷新重试！");
        if (result.IsSuccess) {
            var fileKey = result.Value;
            $("#imageKey").val(fileKey);
            $("#image").attr("src", fileKey);
        } else {
            normalDialog('上传图片出错' + result.Message);
        }
        $("#imageUploadProgress").empty();
    };
    var uploadError = function (obj, code, msg) {
        normalDialog("swf error code: " + code + "\r\nhttp status: " + msg
                + "\r\n请重试！");
        $("#imageUploadProgress").html("");
    };
    var uploadProgress = function (obj, completeBytes, totalBytes) {
        $("#imageUploadProgress").html(
                "正在上传..." + ((completeBytes / totalBytes).toFixed(2) * 100)
                        + "%");
    };
    // 初始化单上传控件
    var initSwfUpload = function () {
        var fileUploadSettings = {
            upload_url: '/upload',
            file_types: '*.jpeg;*.jpg;*.png;*.gif;*.bmp',
            file_types_description: '图片类型',
            button_placeholder_id: 'placeholder',
            file_upload_limit: 0,
            prevent_swf_caching: false,
            button_width: 78,
            button_height: 20,
            button_cursor: SWFUpload.CURSOR.HAND,
            button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
            button_text: '本地上传',
            button_text_left_padding: 10,
            button_text_top_padding: 2,
            file_post_name: "fileData",
            file_dialog_complete_handler: fileDialogComplete,
            upload_success_handler: uploadSuccess,
            upload_progress_handler: uploadProgress,
            // upload_complete_handler: uploadComplete,
            upload_error_handler: uploadError,
            flash_url: '/Content/Scripts/Components/swfupload/swfupload.swf',
            debug: false
        };
        var fileUpload = new SWFUpload(fileUploadSettings);
    };

    var init = function () {
        $("#btnAdd").click(function () {
            location.href = "/book/edit";
        });
        category.initCmb({
            container: $("#category"),
            binder: $("input[name='categoryId']"),
            selectedValue: $("input[name='categoryId']").val()
        });
        $('input.m_btn_date').on('click', function () {
            datePicker();
        });
        $("a[name='deleteLink']").on("click", function () {
            var e = this;
            standardArtDialog.dialog({
                lock: true,
                title: '提示',
                content: '确定要删除该书籍么？',
                button: [
                    {
                        value: '确定',
                        callback: function () {
                            var id = $(e).parent().find("input[type='hidden']").val();                  
                            $.ajax({
                                type: 'get',
                                url: '/book/delete',
                                data: { id: id },
                                success: function (data) {
                                    if (data.IsSuccess && data.Value > 0) {
                                        standardArtDialog.dialog({
                                            lock: true,
                                            title: '提示',
                                            content: '删除成功',
                                            ok: function () { location.reload(); }
                                        });
                                    } else {
                                        normalDialog("删除失败" + data.Message);
                                    }
                                }
                            });
                        }
                    },
                    {
                        value: '取消',
                        focus: true,
                        callback: function () {
                            this.close();
                        }
                    }
                ]
            });
        });
    };
    return {
        initSwfUpload: initSwfUpload,
        init: init
    };
});